package com.ruoyi.teaching.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.teaching.dao.JPositionDao;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.teaching.domain.JPosition;
import com.ruoyi.teaching.service.IJPositionService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 省市县镇村数据Controller
 * 
 * @author ruoyi
 * @date 2022-03-22
 */
@RestController
@RequestMapping("/teaching/position")
public class JPositionController extends BaseController
{
    @Autowired
    private IJPositionService jPositionService;
    @Autowired
    private JPositionDao jPositionDao;

    /**
     * 查询省市县镇村数据列表
     */
    @PreAuthorize("@ss.hasPermi('teaching:position:list')")
    @GetMapping("/list")
    public TableDataInfo list(JPosition jPosition)
    {
        startPage();
        List<JPosition> list = jPositionService.selectJPositionList(jPosition);
        return getDataTable(list);
    }

    /**
     * 获取省市县镇村数据详细信息
     */
    @GetMapping(value = "/{townId}")
    public AjaxResult getVillage(String townId)
    {
        return AjaxResult.success(jPositionDao.findBytownId(townId));
    }
    @GetMapping(value = "/{countyId}")
    public AjaxResult getTown(String countyId)
    {
        return AjaxResult.success(jPositionDao.findByCountyId(countyId));
    }

    @GetMapping(value = "/{cityId}")
    public AjaxResult getCounty(String cityId)
    {
        return AjaxResult.success(jPositionDao.findByCityId(cityId));
    }

//    @PreAuthorize("@ss.hasPermi('teaching:position:query')")
    @GetMapping(value = "/{provinceId}")
    public AjaxResult getCity(String provinceId)
    {
        return AjaxResult.success(jPositionDao.findByProvinceId(provinceId));
    }

    /**
     * 获取省市县镇村数据详细信息
     */
//    @PreAuthorize("@ss.hasPermi('teaching:position:query')")
    @GetMapping(value = "/")
    public AjaxResult getProvince()
    {
        List<JPosition> list = jPositionService.selectProvinceId();
        return AjaxResult.success(list);
    }
    /**
     * 导出省市县镇村数据列表
     */
    @PreAuthorize("@ss.hasPermi('teaching:position:export')")
    @Log(title = "省市县镇村数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, JPosition jPosition)
    {
        List<JPosition> list = jPositionService.selectJPositionList(jPosition);
        ExcelUtil<JPosition> util = new ExcelUtil<JPosition>(JPosition.class);
        util.exportExcel(response, list, "省市县镇村数据数据");
    }

    /**
     * 获取省市县镇村数据详细信息
     */
    @PreAuthorize("@ss.hasPermi('teaching:position:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(jPositionService.selectJPositionById(id));
    }

    /**
     * 新增省市县镇村数据
     */
    @PreAuthorize("@ss.hasPermi('teaching:position:add')")
    @Log(title = "省市县镇村数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody JPosition jPosition)
    {
        return toAjax(jPositionService.insertJPosition(jPosition));
    }

    /**
     * 修改省市县镇村数据
     */
    @PreAuthorize("@ss.hasPermi('teaching:position:edit')")
    @Log(title = "省市县镇村数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody JPosition jPosition)
    {
        return toAjax(jPositionService.updateJPosition(jPosition));
    }

    /**
     * 删除省市县镇村数据
     */
    @PreAuthorize("@ss.hasPermi('teaching:position:remove')")
    @Log(title = "省市县镇村数据", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(jPositionService.deleteJPositionByIds(ids));
    }
}
